package com.pingtai.indent.dao;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.Query;

import com.pingtai.dao.BaseDao;
import com.pingtai.indent.entity.UserBaseIndentType;
import com.pingtai.indent.entity.UserIndentType;
import com.pingtai.user.entity.User;

public class IndentTypeDao extends BaseDao {
	
	public List findAllBaseIndentType(){
		Query query = getSession().createQuery("from UserBaseIndentType where parentBaseIndentType=null order by baseIndentTypeClassify.sort,sort");
		return query.list();
	}
	
	public List findAllBaseIndentType2(){
		Query query = getSession().createQuery("from UserBaseIndentType  order by baseIndentTypeClassify.sort,sort");
		return query.list();
	}
	
	public void saveUserIndentType(UserIndentType userIndentType){
		saveOrUpdate(userIndentType);
	}
	
	public boolean getParentUserIndentTypeIsopen(User user,Integer baseId){
		UserBaseIndentType base = (UserBaseIndentType) getObject(UserBaseIndentType.class, baseId);
		UserBaseIndentType parent = base.getParentBaseIndentType();
		if(parent==null) return true;
		Query query = getSession().createQuery("from UserIndentType where user.userId=:userId and userBaseIndentType.id=:baseId");
		query.setParameter("userId", user.getUserId());
		query.setParameter("baseId", parent.getId());
		List l = query.list();
		if(l!=null&&l.size()>0){
			return true;
		}
		return false;
	}
	
	public void deleteUserIndentType(UserIndentType userIndentType){
		
		Integer baseIndentTypeId = null;
		if(userIndentType.getUserBaseIndentType()!=null){
			baseIndentTypeId = userIndentType.getUserBaseIndentType().getId();
		}
		if(baseIndentTypeId==null){
			baseIndentTypeId = userIndentType.getUserBaseIndentTypeId();
		}
		
		Query query3 = getSession().createQuery("from UserIndentType where user.userId=? and userBaseIndentType.id=?");
		query3.setParameter(0, userIndentType.getUser().getUserId());
		query3.setParameter(1, baseIndentTypeId);
		List l = query3.list();
		while(l!=null&&l.size()>0){
			userIndentType = (UserIndentType) l.get(0);
			Set s = userIndentType.getChildrenIndentType();
			if(s!=null){
				Iterator it = s.iterator();
				while(it.hasNext()){
					UserIndentType t = (UserIndentType) it.next();
					deleteUserIndentType(t);
				}
			}
			Query query2 = getSession().createQuery("delete from UserIndentType where user.userId=? and parentIndentType.id=?");
			query2.setParameter(0, userIndentType.getUser().getUserId());
			query2.setParameter(1, userIndentType.getId());
			query2.executeUpdate();
			break;
		}
		
		Query query = getSession().createQuery("delete from UserIndentType where user.userId=? and userBaseIndentType.id=?");
		query.setParameter(0, userIndentType.getUser().getUserId());
		query.setParameter(1, baseIndentTypeId);
		query.executeUpdate();
	}
	
	public List findUserIndentType(User user,Integer baseId){
		Query query = getSession().createQuery("from UserIndentType where user.userId=? and userBaseIndentType.id=?");
		query.setParameter(0, user.getUserId());
		query.setParameter(1, baseId);
		return query.list();
	}
	
	public List findUserIndnetType(User user){
		Query query = getSession().createQuery("from UserIndentType where user.userId=:userId ");
		query.setParameter("userId", user.getUserId());
		return query.list();
	}
	
	public List findUserIndentType2(User user,Integer baseId){
		Query query = getSession().createQuery("from UserIndentType where user.userId=? and userBaseIndentType.id=?");
		query.setParameter(0, user.getUserId());
		query.setParameter(1, baseId);
		return query.list();
	}

}
